package com.woniuxy.mycollection;

/**
 * description: SxtArrayList <br>
 * date: 2022/2/2 11:40 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 实现ArrayList
 *
 * @param <E>
 */
public class SxtArrayList<E> {
    private Object[] elementData;
    private int size;

    private static final int DEFALT_CAPACITY = 10;

    public SxtArrayList() {
        elementData = new Object[DEFALT_CAPACITY];
    }

    public SxtArrayList(int capacity) {
        elementData = new Object[capacity];
    }

    public void add(E element) {
        elementData[size++] = element;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(elementData[i] + ",");
        }
        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }

    public static void main(String[] args) {
        SxtArrayList s1 = new SxtArrayList(20);
        s1.add("aa");
        s1.add("bb");
        System.out.println(s1);
    }
}
